﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;

namespace DiagramDesigner
{

    public class PointLine : Shape, ISelectable, IAdornable, INotifyPropertyChanged
    {
        LineGeometry linegeo = new LineGeometry();
        PointLineAdorner adorn;
        public PointLine():
            base()
        {
            adorn = new PointLineAdorner(this);  
            
        }        

        //void PointLine_SizeChanged(object sender, SizeChangedEventArgs e)
        //{
        //    //if (e.Source != this)
        //    {
        //        double dx = e.NewSize.Width - Math.Abs(EndPoint.X - StartPoint.X);
        //        double dy = e.NewSize.Height - Math.Abs(EndPoint.Y - StartPoint.Y);
        //        EndPoint = new Point(EndPoint.X + dx, EndPoint.Y + dy);
        //        e.Handled = true;
        //    }
        //}

        //protected override void OnMouseEnter(System.Windows.Input.MouseEventArgs e)
        //{
        //    Stroke = new SolidColorBrush(Colors.Red);
        //    base.OnMouseEnter(e);
        //}

        //protected override void OnMouseLeave(System.Windows.Input.MouseEventArgs e)
        //{
        //    Stroke = new SolidColorBrush(Colors.Black);            
        //    base.OnMouseLeave(e);
        //}

        //protected override void OnGotFocus(RoutedEventArgs e)
        //{
        //    MessageBox.Show("GotFocus");
        //    base.OnGotFocus(e);
        //}

        //protected override void OnMouseDown(System.Windows.Input.MouseButtonEventArgs e)
        //{
        //    MessageBox.Show("Man down!");
        //    base.OnMouseDown(e);
        //}

        // Dependency properties
        public static readonly DependencyProperty StartPointProperty =
            LineGeometry.StartPointProperty.AddOwner(
                typeof(PointLine),
                new FrameworkPropertyMetadata(new Point(0, 0),
                    FrameworkPropertyMetadataOptions.AffectsMeasure));

        public static readonly DependencyProperty EndPointProperty =
            LineGeometry.EndPointProperty.AddOwner(
                typeof(PointLine),
                new FrameworkPropertyMetadata(new Point(0, 0),
                    FrameworkPropertyMetadataOptions.AffectsMeasure));

        //public new static readonly DependencyProperty StretchProperty =
        //    DependencyProperty.Register("StretchProperty", typeof(Stretch),
        //        typeof(PointLine),
        //        new FrameworkPropertyMetadata(Stretch.None,
        //            FrameworkPropertyMetadataOptions.AffectsMeasure));

        //private static void WidthPropertyChangedCallback(DependencyObject obj,
        //  DependencyPropertyChangedEventArgs e)
        //{
        //    double dw = (double)e.OldValue - (double)e.NewValue;

        //    PointLine p = ((PointLine)obj);
        //    p.StartPoint.Offset(dw/2, 0);
        //    p.EndPoint.Offset(-dw / 2, 0);
        //}

        //public new static readonly DependencyProperty HeightProperty =
        //    Shape.HeightProperty.AddOwner(
        //        typeof(PointLine),
        //        new FrameworkPropertyMetadata(new Point(0, 0),
        //            FrameworkPropertyMetadataOptions.AffectsMeasure, HeightPropertyChangedCallback));

        //private static void HeightPropertyChangedCallback(DependencyObject obj,
        //  DependencyPropertyChangedEventArgs e)
        //{
        //    double dh = (double)e.OldValue - (double)e.NewValue;

        //    PointLine p = ((PointLine)obj);
        //    p.StartPoint.Offset(0,dh / 2);
        //    p.EndPoint.Offset(0,-dh / 2);
        //}


        //public new Stretch Stretch
        //{
        //    set
        //    {
        //        SetValue(StretchProperty, Stretch.None);            
        //    }
        //    get
        //    {
        //        return (Stretch)GetValue(StretchProperty);
        //    }
        //}

        public Point StartPoint
        {
            set { SetValue(StartPointProperty, value); }
            get { return (Point)GetValue(StartPointProperty); }
        }

        public Point EndPoint
        {
            set { SetValue(EndPointProperty, value); }
            get { return (Point)GetValue(EndPointProperty); }
        }
        
        // Required DefiningGeometry override
        protected override Geometry DefiningGeometry
        {
            get
            {
                linegeo.StartPoint = StartPoint;
                linegeo.EndPoint = EndPoint;
                return linegeo;
            }
        }
        

        public System.Windows.Documents.Adorner GetAdorner()
        {            
            return adorn;
            //return null;
        }        
        

        //public bool IsSelected {get; set;}

        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// The <see cref="IsSelected" /> property's name.
        /// </summary>
        public const string IsSelectedPropertyName = "IsSelected";

        private bool _isSelected = false;

        /// <summary>
        /// Sets and gets the IsSelected property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public bool IsSelected
        {
            get
            {
                return _isSelected;
            }

            set
            {
                if (_isSelected == value)
                {
                    return;
                }

                _isSelected = value;
                RaisePropertyChanged(IsSelectedPropertyName);
            }
        }

        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        //protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters)
        //{
        //    Point hit = hitTestParameters.HitPoint;

        //    double dx = (EndPoint.X - StartPoint.X);
        //    double dy = (EndPoint.Y - StartPoint.Y);
        //    double dist = (-dy * hit.X + dx * hit.Y +
        //        StartPoint.X * EndPoint.Y - EndPoint.X * StartPoint.Y) /
        //        Math.Sqrt(dx * dx + dy * dy);

        //    if (Math.Abs(dist) < 3)
        //        return new PointHitTestResult(this, hit);

        //    return base.HitTestCore(hitTestParameters);
        //}

    }

 
    public interface IAdornable
    {
        System.Windows.Documents.Adorner GetAdorner();
    }

    //public interface IResizeable
    //{
        
    //}

}
